package com.taotao.web;

import com.taotao.SystemConstants;
import com.taotao.TaotaoResult;
import com.taotao.pojo.SearchResult;
import com.taotao.service.SearchService;
import com.taotao.service.TbItemSolrManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author evader
 * @date 2020-09-30 09:52
 */
@RestController
@RequestMapping("/solr/manager")
public class SearchController {
    @Autowired
    private TbItemSolrManagerService solrManagerService;

    @Autowired
    private SearchService searchService;

    @CrossOrigin
    @RequestMapping("/import_item")

    public TaotaoResult importTbItem2Solr() {
        TaotaoResult result = solrManagerService.importAllTbItem2Solr();
        return result;
    }



    @RequestMapping("/search")

    public TaotaoResult search(@RequestParam("q") String keywords, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "30") Integer pageSize) {
        SearchResult result = searchService.search(keywords, pageNum, pageSize);
        if(result == null) {
            return TaotaoResult.build(SystemConstants.TAOTAO_RESULT_STATUS_ERROR, "搜索异常");
        }
        return TaotaoResult.ok(result);
    }


}
